﻿using Newtonsoft.Json;
using PineLake.Drmwe.Lib.JsonSerializer.JsonNullToEmpty;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PineLake.Drmwe.Lib.JsonSerializer
{
    public class HttpResponseExtension
    {
        public static HttpResponseMessage ToJson(Object obj)
        {
            String Str = string.Empty;
            if (obj is String || obj is Char)
            {
                Str = obj.ToString();
            }
            else
            {
                //替换NULL为"" 格式化日期
                var JsonSetting = new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss", ContractResolver = new NullToEmptyStringResolver() };
                Str = JsonConvert.SerializeObject(obj, JsonSetting);
            }
            HttpResponseMessage result = new() { Content = new StringContent(Str, Encoding.GetEncoding("UTF-8"), "application/json") };
            return result;
        }

        public static string ObjToJson(Object obj)
        {
            String Str = "";
            if (obj != null)
            {
                if (obj is String || obj is Char)
                {
                    Str = obj.ToString();
                }
                else
                {
                    //替换NULL为"" 格式化日期
                    var JsonSetting = new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss", ContractResolver = new NullToEmptyStringResolver() };
                    Str = JsonConvert.SerializeObject(obj, JsonSetting);
                }
            }
            return Str;
        }
    }
}
